<template>
  <div>
    <div style="margin: 10px 0">
      <el-input style="width: 200px" placeholder="请输入标题" suffix-icon="el-icon-search" v-model="name"></el-input>
      <!--      <el-input style="width: 200px" placeholder="请输入" suffix-icon="el-icon-message" class="ml-5" v-model="email"></el-input>-->
      <!--      <el-input style="width: 200px" placeholder="请输入" suffix-icon="el-icon-position" class="ml-5" v-model="address"></el-input>-->
      <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
      <el-button type="warning" @click="reset">重置</el-button>
    </div>

    <template v-if="role === 'ROLE_ADMIN'">
      <div style="margin: 10px 0">
        <el-button type="primary" @click="handleAdd">新增 <i class="el-icon-circle-plus-outline"></i></el-button>
        <el-popconfirm
            class="ml-5"
            confirm-button-text='确定'
            cancel-button-text='我再想想'
            icon="el-icon-info"
            icon-color="red"
            title="您确定批量删除这些数据吗？"
            @confirm="delBatch"
        >
          <el-button type="danger" slot="reference">批量删除 <i class="el-icon-remove-outline"></i></el-button>
        </el-popconfirm>
        <el-upload action="http://localhost:9090/announcements/import" :show-file-list="false" accept="xlsx" :on-success="handleExcelImportSuccess" style="display: inline-block">
          <el-button type="primary" class="ml-5">导入 <i class="el-icon-bottom"></i></el-button>
        </el-upload>
        <el-button type="primary" @click="exp" class="ml-5">导出 <i class="el-icon-top"></i></el-button>
      </div>
      <el-table :data="tableData" border stripe :header-cell-class-name="'header'"  @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" key="'sysSelect"></el-table-column>
        <el-table-column
            type="index"
            key="sysIndex"
            width="50">
        </el-table-column>
        <el-table-column prop="name" label="公告标题" key="sysName" width="150" :show-overflow-tooltip='true'></el-table-column>
        <el-table-column prop="priority" label="优先级" sortable width="145">
          <template slot-scope="scope" >
            <el-rate
                v-model="scope.row.priority"
                :colors="colors"
                disabled>
            </el-rate>
          </template>
        </el-table-column>
        <el-table-column prop="content" label="公告内容" :show-overflow-tooltip='true'></el-table-column>
        <el-table-column prop="createName" label="发布者" sortable width="90"></el-table-column>
        <el-table-column prop="createTime" label="发布时间" sortable width="120" :show-overflow-tooltip='true'></el-table-column>
        <el-table-column prop="updateName" label="修改者" width="70"></el-table-column>
        <el-table-column prop="updateTime" label="修改时间" width="80" key="updateTime" :show-overflow-tooltip='true'></el-table-column>
        <el-table-column prop="type" label="是否发布" sortable width="120">
          <template slot-scope="scope" >
            <el-switch v-model="scope.row.type"
                       :active-value="1"
                       :inactive-value="0"
                       @change="changeType(scope.row)">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作"  width="280" align="center">
          <template slot-scope="scope">
            <el-button type="success" @click="handleEdit(scope.row)">编辑 <i class="el-icon-edit"></i></el-button>
            <el-popconfirm
                class="ml-5"
                confirm-button-text='确定'
                cancel-button-text='我再想想'
                icon="el-icon-info"
                icon-color="red"
                title="您确定删除吗？"
                @confirm="del(scope.row.id)"
            >
              <el-button type="danger" slot="reference">删除 <i class="el-icon-remove-outline"></i></el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <div style="padding: 10px 0">
        <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pageNum"
            :page-sizes="[2, 5, 10, 20]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
        </el-pagination>
      </div>
      <el-dialog title="公告信息" :visible.sync="dialogFormVisible" width="30%" >
        <el-form label-width="80px" size="small">
          <el-form-item label="公告标题.">
            <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="优先级" prop="priority">
            <el-rate v-model="form.priority" :colors="colors"></el-rate>
          </el-form-item>

          <el-form-item label="公告内容">
            <el-input v-model="form.content" autocomplete="off"></el-input>
          </el-form-item>
<!--          <el-form-item label="发布者">-->
<!--            <el-input v-model="form.createName" disabled="disabled" autocomplete="off"></el-input>-->
<!--          </el-form-item>-->
          <el-form-item label="发布时间">
            <el-date-picker v-model="form.createTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期时间"></el-date-picker>
          </el-form-item>
<!--          <el-form-item label="修改者">-->
<!--            <el-input v-model="form.updateName" disabled="disabled" autocomplete="off"></el-input>-->
<!--          </el-form-item>-->
          <el-form-item label="修改时间">
            <el-date-picker v-model="form.updateTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期时间"></el-date-picker>
          </el-form-item>
          <!--        <el-form-item label="是否发布">-->
          <!--&lt;!&ndash;          <el-input v-model="form.type" autocomplete="off"></el-input>&ndash;&gt;-->
          <!--          <el-switch v-model="form.type"></el-switch>-->
          <!--        </el-form-item>-->

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="save">确 定</el-button>
        </div>
      </el-dialog>
    </template>
    <template v-else>
      <el-table :data="tableData" border stripe :header-cell-class-name="'header'">
        <el-table-column fixed="left" type="index" width="50" key="userIndex"></el-table-column>
        <el-table-column prop="name" label="公告标题" width="150" :show-overflow-tooltip='true'></el-table-column>
        <el-table-column prop="priority" label="优先级" sortable width="145">
          <template slot-scope="scope" >
            <el-rate
                v-model="scope.row.priority"
                :colors="colors"
                disabled>
            </el-rate>
          </template>
        </el-table-column>
        <el-table-column prop="content" label="公告内容" :show-overflow-tooltip='true'></el-table-column>
        <el-table-column prop="createTime" label="发布时间" sortable width="120" :show-overflow-tooltip='true'></el-table-column>
      </el-table>
      <div style="padding: 10px 0">
        <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pageNum"
            :page-sizes="[2, 5, 10, 20]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
        </el-pagination>
      </div>
    </template>

  </div>
</template>

<script>
export default {
  name: "Announcements",
  data() {
    return {
      colors:['#99A9BF', '#F7BA2A', '#EB5E26'],
      tableData: [],
      role:'',
      total: 0,
      pageNum: 1,
      pageSize: 10,
      name: "",
      form: {},
      dialogFormVisible: false,
      multipleSelection: [],
      user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {}
    }
  },
  created() {
    this.load()
  },
  methods: {
    load() {
      if (this.user.role === 'ROLE_ADMIN'){
        this.request.get("/announcements/page", {
          params: {
            pageNum: this.pageNum,
            pageSize: this.pageSize,
            name: this.name,
          }
        }).then(res => {
          this.tableData = res.data.records
          this.role = this.user.role

          console.log(this.tableData)

          this.total = res.data.total
        })
        console.log("user:",this.user)//this.user.role
        console.log("role:",this.user.role)
      }else {
        this.request.get("/announcements/frontPage", {
          params: {
            pageNum: this.pageNum,
            pageSize: this.pageSize,
            name: this.name,
          }
        }).then(res => {
          this.tableData = res.data.records
          this.role = this.user.role
          this.total = res.data.total
        })
      }
    },
    save() {
      console.log("test:", this.form)

      this.request.post("/announcements", this.form).then(res => {
        if (res.code === '200') {
          this.$message.success("保存成功")
          this.dialogFormVisible = false
          this.load()
        } else {
          this.$message.error("保存失败")
        }
      })
    },
    changeType(row) {
      this.request.post("/announcements", row).then(res => {
        if (res.code === '200') {
          this.$message.success("修改成功")
        }
      })
    },
    handleAdd() {
      this.dialogFormVisible = true
      this.form = {}
      this.form.createName = this.user.username
      this.form.updateName = ""

      this.$nextTick(() => {
        if(this.$refs.img) {
           this.$refs.img.clearFiles();
         }
         if(this.$refs.file) {
          this.$refs.file.clearFiles();
         }
      })
    },
    handleEdit(row) {
      this.form = JSON.parse(JSON.stringify(row))
      this.form.updateName = this.user.username

      this.dialogFormVisible = true
       this.$nextTick(() => {
         if(this.$refs.img) {
           this.$refs.img.clearFiles();
         }
         if(this.$refs.file) {
          this.$refs.file.clearFiles();
         }
       })
    },
    del(id) {
      this.request.delete("/announcements/" + id).then(res => {
        if (res.code === '200') {
          this.$message.success("删除成功")
          this.load()
        } else {
          this.$message.error("删除失败")
        }
      })
    },
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = val
    },
    delBatch() {
      let ids = this.multipleSelection.map(v => v.id)  // [{}, {}, {}] => [1,2,3]
      this.request.post("/announcements/del/batch", ids).then(res => {
        if (res.code === '200') {
          this.$message.success("批量删除成功")
          this.load()
        } else {
          this.$message.error("批量删除失败")
        }
      })
    },
    reset() {
      this.name = ""
      this.load()
    },
    handleSizeChange(pageSize) {
      console.log(pageSize)
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      console.log(pageNum)
      this.pageNum = pageNum
      this.load()
    },
    handleFileUploadSuccess(res) {
      this.form.file = res
    },
    handleImgUploadSuccess(res) {
      this.form.img = res
    },
    download(url) {
      window.open(url)
    },
    exp() {
      window.open("http://localhost:9090/announcements/export")
    },
    handleExcelImportSuccess() {
      this.$message.success("导入成功")
      this.load()
    }
  }
}
</script>


<style>
/*.headerBg {*/
/*  background: #eee!important;*/
/*}*/
.header{
  /*background: #eee!important;*/
  color: rgb(102, 102, 102);
  font-size: 14px;
}
.el-tooltip__popper{
  max-width:20%;
}
.el-tooltip__popper,.el-tooltip__popper.is-dark{
  background:rgb(48, 65, 86) !important;
  color: #fff !important;
  line-height: 24px;
}
</style>
